import {createRouter, createWebHashHistory} from 'vue-router'
import Index from '~/pages/index.vue'
import Login from '~/pages/login.vue'
import NotFound from '~/pages/404.vue'
import Admin from '~/layout/admin.vue'
import GoodList from '~/pages/goods/list.vue'
import CategoryList from '~/pages/category/list.vue'
import ImageList from '~/pages/image/list.vue'
import NoticeList from '~/pages/notice/list.vue'
import ManagerList from '~/pages/manager/list.vue'
import AccessList from '~/pages/access/list.vue'
import RoleList from '~/pages/role/list.vue'
import UserList from '~/pages/user/list.vue'
import LevelList from '~/pages/level/list.vue'

// 默认路由，所有用户共享
const routes = [
    {
        path: "/",
        name: "admin",
        component: Admin,
    },
    {
        path: "/login",
        component: Login,
        meta: {
            title: "登录页"
        }
    }, {
        path: '/:pathMatch(.*)*',
        name: 'NotFound',
        component: NotFound
    }]

// 动态路由，用于匹配菜单动态添加路由
const asyncRoutes = [{
    path: "/",
    name: "/",
    component: Index,
    meta: {
        title: "后台首页"
    }
}, {
    path: "/goods/list",
    name: "/goods/list",
    component: GoodList,
    meta: {
        title: "商品管理"
    }
}, {
    path: "/category/list",
    name: "/category/list",
    component: CategoryList,
    meta: {
        title: "分类列表"
    }
},
    {
        path: "/image/list",
        name: "/image/list",
        component: ImageList,
        meta: {
            title: "图库列表"
        }
    },
    {
        path: "/notice/list",
        name: "/notice/list",
        component: NoticeList,
        meta: {
            title: "公告列表"
        }
    },
    {
        path: "/manager/list",
        name: "/manager/list",
        component: ManagerList,
        meta: {
            title: "管理员管理"
        }
    },
    {
        path:"/access/list",
        name:"/access/list",
        component:AccessList,
        meta:{
            title:"菜单权限管理"
        }
    },{
        path:"/role/list",
        name:"/role/list",
        component:RoleList,
        meta:{
            title:"角色管理"
        }
    },
    {
        path:"/user/list",
        name:"/user/list",
        component:UserList,
        meta:{
            title:"用户列表"
        }
    },
    {
        path:"/level/list",
        name:"/level/list",
        component:LevelList,
        meta:{
            title:"会员等级"
        }
    }
]


export const router = createRouter({
    history: createWebHashHistory(),
    routes
})

// 动态添加路由的方法
export function addRoutes(menus) {
    // 是否有新的路由
    let hasNewRoutes = false
    const findAndAddRoutesByMenus = (arr) => {
        arr.forEach(e => {
            //在匹配数组中查询是否已经存在，根据路由路径匹配
            let item = asyncRoutes.find(o => o.path == e.frontpath)
            //不存在的话则添加路由
            if (item && !router.hasRoute(item.path)) {
                router.addRoute("admin", item)
                hasNewRoutes = true
            }
            //如果存在子路由则开始递归
            if (e.child && e.child.length > 0) {
                findAndAddRoutesByMenus(e.child)
            }
        })
    }
    //调用路由
    findAndAddRoutesByMenus(menus)
    return hasNewRoutes
}
